﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twodi.Arguments;
using Microsoft.Xna.Framework.Input;

namespace Twodi.Input.Events
{
    /// <summary>
    /// Tipos de eventos do mouse.
    /// </summary>
    public enum MouseEventsType
    {
        Anytime,
        Click,
        Pressed,
        Moved
    }

    /// <summary>
    /// Evento de mouse.
    /// </summary>
    public class MouseEvent : AbstractInputEvent<MouseEventsType, MouseInput>
    {
        /// <summary>
        /// Botões do mouse.
        /// </summary>
        protected MouseButton[] buttons;

        public MouseEvent(MouseEventsType type, params MouseButton[] buttons)
            : base(type)
        {
            this.Buttons = buttons;
        }

        public MouseEvent(MouseEventsType type, InputEventHandler<InputEventArgs<MouseInput>> method, params MouseButton[] buttons)
            : base(type, method)
        {
            this.Buttons = buttons;
        }

        private delegate bool Checker(MouseButton button);
        public override bool Handle(MouseInput input)
        {
            bool result = true;
            int index = 0;
            Checker method = null;

            switch (Type)
            {
                case MouseEventsType.Anytime:
                    return onEvents(new InputEventArgs<MouseInput>(input));
                case MouseEventsType.Click:
                    method = input.IsButtonClick;
                    break;
                case MouseEventsType.Pressed:
                    method = input.IsButtonPressed;
                    break;
                case MouseEventsType.Moved:
                    if (input.Moved) return onEvents(new InputEventArgs<MouseInput>(input));
                    return true;
            }

            while (result && method != null)
            {
                if (index == buttons.Length) break;
                result = method(buttons[index]);
                index++;
            }

            if (result) return onEvents(new InputEventArgs<MouseInput>(input));
            return true;
        }

        public MouseButton[] Buttons
        {
            get { return this.buttons; }
            set
            {
                this.buttons = value;
            }
        }
    }
}
